package com.qn.utils.excel;

import com.qn.annotations.PropertyName;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.List;

public class XSSF implements Excel {
    private XSSFSheet sheet;

    public XSSF() {

    }

    /**
     * 构造函数，初始化excel数据
     *
     * @param filePath  excel路径
     * @param sheetName sheet表名
     */
    @Override
    public void getSheet(String filePath, String sheetName) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            //获取sheet
            this.sheet = sheets.getSheet(sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据行和列的索引获取单元格的数据
     *
     * @param row    行
     * @param column 列
     * @return 指向的行列值
     */
    @Override
    public String getExcelDateByIndex(int row, int column) {
        XSSFRow row1 = sheet.getRow(row);
        String cell = row1.getCell(column).toString();
        return cell;
    }

    @Override
    public List<String> getAllSheetNames(String filePath) {
        FileInputStream fileInputStream = null;
        List<String> sheetNames = new ArrayList<>();
        try {
            fileInputStream = new FileInputStream(filePath);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            for (int i = 0; i < sheets.getNumberOfSheets(); i++) {//获取每个Sheet表
                sheetNames.add(sheets.getSheetName(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sheetNames;
    }

    /**
     * 根据某一列值为“******”的这一行，来获取该行第x列的值
     *
     * @param caseName      没用
     * @param currentColumn 当前单元格列的索引
     * @param targetColumn  目标单元格列的索引
     * @return 所指向的列的值
     */
    @Override
    public String getCellByCaseName(String caseName, int currentColumn, int targetColumn) {
        String operateSteps = "";
        //获取行数
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 0; i < rows; i++) {
            XSSFRow row = sheet.getRow(i);
            String cell = row.getCell(currentColumn).toString();
            if (cell.equals(caseName)) {
                operateSteps = row.getCell(targetColumn).toString();
                break;
            }
        }
        return operateSteps;
    }

    /**
     * @param object 任意对象，该对象为Excel表格对应的对象
     * @return 返回一个被Excel表格数据填充的集合
     */
    @Override
    public <T> List<T> depositExcelData(T object, int initRow) throws Exception {
        int rows = sheet.getPhysicalNumberOfRows();
        Class aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        List<T> companyDataList = new LinkedList<>();
        // 属性指针
        int fieldIndex = 2;
        // 列错误时的指针
        int cellIndex = 0;
        // 是否出错
        boolean flag = true;

        for (int i = 2; i < rows; i++) {
            XSSFRow row = sheet.getRow(i);
            T obj = (T) aClass.newInstance();
            for (int j = 0; j < 15; j++) {
                Field field = fields[fieldIndex];
                field.setAccessible(true);
                PropertyName annotation = field.getAnnotation(PropertyName.class);
                if (getExcelDateByIndex(1, j).equals(annotation.name())) {
                    field.set(obj, row.getCell(j).toString());
                    fieldIndex++;
                    j = cellIndex;
                    flag = true;
                } else {
                    if (flag) {
                        cellIndex = j + 1;
                        flag = false;
                    }
                }
            }

            cellIndex = 0;
            fieldIndex = 2;
            companyDataList.add(obj);
        }
        return companyDataList;
    }
}
